package cz.zcu.fav.rat.managed.bean;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



//public class DataServiceFactory
//{
//	@SuppressWarnings("unchecked")
//	public static <T> T getBean(String beanName)
//	{
//		return (T) FacesContext
//				.getCurrentInstance()
//				.getApplication()
//				.getELResolver()
//				.getValue(FacesContext.getCurrentInstance().getELContext(),
//						null, beanName);
//	}
//
//}

public class SpringBeanFactory {
	 private static final SpringBeanFactory instance = new SpringBeanFactory(); 
	 private WebApplicationContext context;

	 public SpringBeanFactory() {
	  FacesContext fc = FacesContext.getCurrentInstance();
	  ServletContext sc = (ServletContext) fc.getExternalContext().getContext();
	  this.context = WebApplicationContextUtils.getWebApplicationContext(sc);
	 }
	 
	 public static SpringBeanFactory getInstance() {
	  return instance;
	 }

	 public Object getBean(String name) {
	  return this.context.getBean(name);
	 }

	 public <T> T getBean(Class<T> requiredType) {
	  return this.context.getBean(requiredType);
	 }

	 public <T> T getBean(String name, Class<T> requiredType) {
	  return this.context.getBean(name, requiredType);
	 }

	 public WebApplicationContext getContext() {
	  return this.context;
	 }
	}
